﻿// --------------------------------
// <copyright file="ScrollPanel.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>20. 11. 2011 15:08:25</date>

// <summary>
//
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using FITClub.Util;

namespace FITClub.TextureMenu
{
#if WINDOWS

    [System.Serializable]
#endif
    public class ScrollPanel : ADisplayable, IUpdateableItem
    {
        public enum ScrollDirection { DownUp, UpDown, LeftRight, RightLeft }

#if WINDOWS

        [Category("Design")]
#endif
        public int Start { get; set; }

#if WINDOWS

        [Category("Design")]
#endif
        public int End { get; set; }

        [XmlArrayItem(typeof(Label))]
#if WINDOWS
        [Category("Content")]
#endif
        public List<ADisplayable> Items { get; set; }

        [XmlAttribute]
#if WINDOWS
        [Category("Design")]
#endif
        public float Speed { get; set; }

        [XmlAttribute]
#if WINDOWS
        [Category("Design")]
#endif
        public ScrollDirection Direction { get; set; }

        private float remainder = 0;

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (this.ContentLoaded == false)
            {
                foreach (var item in this.Items)
                {
                    item.LoadContent(loader);
                }
            }
        }

        public override void Dispose()
        {
            if (this.ContentLoaded)
            {
                foreach (var item in this.Items)
                {
                    item.Dispose();
                }
            }
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.Color tint)
        {
            foreach (var item in this.Items)
            {
                bool draw = true;
                //switch (Direction)
                //{
                //    case ScrollDirection.DownUp:
                //        draw = item.Bounds.Y > this.Bounds.Y;
                //        break;
                //    ///todo: dokoncit
                //}
                if (draw) item.Draw(spriteBatch, tint);
            }
        }

        public void Update(Microsoft.Xna.Framework.GameTime gt)
        {
            remainder += this.Speed * ((float)gt.ElapsedGameTime.Milliseconds) / 1000.0f;
            int delta = (int)remainder;
            if (delta == 0)
            {
                return;
            }
            remainder = remainder - (float)delta;
            if (Direction == ScrollDirection.DownUp || Direction == ScrollDirection.RightLeft)
            {
                delta *= -1;
            }

            foreach (var item in this.Items)
            {
                if (Direction == ScrollDirection.DownUp || Direction == ScrollDirection.UpDown)
                {
                    item.Bounds = item.Bounds.OffsetXY(0, delta);
                    if (Direction == ScrollDirection.DownUp && item.Bounds.Y - this.Bounds.Y < this.End)
                    {
                        item.Bounds = item.Bounds.ResetY(this.Bounds.Bottom);
                    }
                    if (Direction == ScrollDirection.UpDown && item.Bounds.Y - this.Bounds.Y > this.End)
                    {
                        item.Bounds = item.Bounds.ResetY(this.Bounds.Y);
                    }
                }
                else
                {
                    //// @todo dokoncit
                    item.Bounds.OffsetXY(delta);
                    if (item.Bounds.X - this.Bounds.X > this.End)
                    {
                    }
                }
            }
        }

#if WINDOWS_PHONE == flase

        public override void Resized(double ratioX, double ratioY)
        {
            base.Resized(ratioX, ratioY);
            if (this.Direction == ScrollDirection.UpDown || this.Direction == ScrollDirection.DownUp)
            {
                this.Start = (int)Math.Ceiling(ratioY * this.Start);
                this.End = (int)Math.Ceiling(ratioY * this.End);
            }
            else
            {
                this.Start = (int)Math.Ceiling(ratioX * this.Start);
                this.End = (int)Math.Ceiling(ratioX * this.End);
            }
            foreach (var item in this.Items)
            {
                item.Resized(ratioX, ratioY);
            }
        }

#endif

        public override void Move(Microsoft.Xna.Framework.Point delta)
        {
            base.Move(delta);
            this.Items.ForEach((ADisplayable ad) => ad.Move(delta));
        }
    }
}